home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C# & Game Programming - A…er's Guide (2nd Edition)
/
Buono 2nd Ed.iso
/
Chapter5
/
5.37
/
5.37.cs
next >
Wrap
Text File
|
2004-09-07
|
3KB
|
130 lines
/* Linking classes with keywords overide and virtual. */
using System;
namespace Chapter5 {
public class Area {
public short Base, Height;
public double Radius;
public short Base2;
public Area() {}
public Area(short w, short h) {
Base = w;
Height = h;
}
public Area(double r) {
Radius = r;
}
public Area(short a, short b, short h) {
Base = a;
Base2 = b;
Height = h;
}
public static short ReadBase(Area X) {
return(X.Base);
}
public static void SetBase(Area X, short a) {
X.Base = a;
}
public static void SetBase2(Area X, short b) {
X.Base2 = b;
}
public static short ReadHeight(Area X) {
return(X.Height);
}
public static void SetHeight(Area X, short h) {
X.Height = h;
}
public static double ReadRadius(Area X) {
return(X.Radius);
}
public static void SetRadius(Area X, double r) {
X.Radius = r;
}
public static double SumOfBases(Area X) {
return(X.Base+X.Base2);
}
public virtual void CalculateArea() {
Console.WriteLine("No Shape Found\n");
}
}
public class Parallelogram: Area {
public Parallelogram(short w, short l) {
SetBase(this, w);
SetHeight(this, l);
}
override public void CalculateArea() {
Console.WriteLine("Area = " + ReadBase(this) *
ReadHeight(this));
}
}
public class Triangle : Area {
public Triangle(short w, short l) {
SetBase(this, w); SetHeight(this, l);}
override public void CalculateArea() {
Console.WriteLine("\n Area = " + (ReadBase(this) *
ReadHeight(this)) / 2);}
}
public class Circle : Area {
public double PI = 3.1415926535897932384626433832795;
public Circle(double r) {
SetRadius(this, r);
}
override public void CalculateArea() {
Console.WriteLine("\n Area = " + (PI * ReadRadius(this) *
ReadRadius(this)));
}
}
public class Trapezoid : Area {
public Trapezoid(short a, short b, short h) {
SetBase(this, a);
SetBase2(this, b);
SetHeight(this, h);
}
override public void CalculateArea() {
Console.WriteLine("\n Area = " + ReadHeight(this) *
(SumOfBases(this)) / 2);
}
public static void CalculateArea(Area Relay) {
Relay.CalculateArea();
}
static void Main() {
Area Shape = new Area();
Parallelogram Shape1 = new Parallelogram(5, 5);
CalculateArea(Shape1);
Parallelogram Shape2 = new Parallelogram(5, 6);
CalculateArea(Shape2);
Triangle Shape3 = new Triangle(5, 6);
CalculateArea(Shape3);
Circle Shape4 = new Circle(3);
CalculateArea(Shape4);
Trapezoid Shape5 = new Trapezoid(4, 5, 6);
CalculateArea(Shape5);
}
}
}